#!/bin/bash

# Remote Compilation Script
# Compiles the project on a remote host
# Usage: ./remote-compile.sh <host> <user> <password> [build-type]

set -e

REMOTE_HOST="${1:-192.168.10.11}"
REMOTE_USER="${2:-asr}"
REMOTE_PASSWORD="${3:-123456}"
BUILD_TYPE="${4:-full}"  # full, cpp, go, test

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_error() { echo -e "${RED}[✗]${NC} $1"; }

REMOTE_PATH="/home/asr/git/goyolopenvino"

log_info "Remote Compilation Script"
log_info "Target: ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_PATH}"
log_info "Build Type: ${BUILD_TYPE}"
echo ""

# Create compilation script
COMPILE_SCRIPT=$(mktemp)

cat > "$COMPILE_SCRIPT" << 'COMPILE_EOF'
#!/bin/bash
set -e

REMOTE_PATH="/home/asr/git/goyolopenvino"
BUILD_TYPE="${1:-full}"

# Set OpenVINO environment
export OPENVINO_INCLUDE_DIR=/opt/intel/openvino_genai_ubuntu24_2025.3.0.0_x86_64/runtime/include
export OPENVINO_LIB_DIR=/opt/intel/openvino_genai_ubuntu24_2025.3.0.0_x86_64/runtime/lib/intel64

echo "=== Environment Setup ==="
echo "OPENVINO_INCLUDE_DIR: $OPENVINO_INCLUDE_DIR"
echo "OPENVINO_LIB_DIR: $OPENVINO_LIB_DIR"
echo ""

# Build C++
if [ "$BUILD_TYPE" = "full" ] || [ "$BUILD_TYPE" = "cpp" ]; then
    echo "=== Building C++ Component ==="
    cd "$REMOTE_PATH/YOLOv8-OpenVINO-CPP-Inference"
    
    echo "Cleaning..."
    rm -rf build bin
    
    echo "Creating build directory..."
    mkdir -p build
    
    echo "Running CMake..."
    cd build
    cmake .. -DCMAKE_BUILD_TYPE=Release \
        -DOPENVINO_INCLUDE_DIR="$OPENVINO_INCLUDE_DIR" \
        -DOPENVINO_LIB_DIR="$OPENVINO_LIB_DIR"
    
    echo "Compiling..."
    make -j$(nproc)
    
    echo "Creating bin directory..."
    mkdir -p ../bin
    cp detect ../bin/
    cp detect_service ../bin/
    
    echo "✓ C++ build completed"
    echo ""
fi

# Build Go
if [ "$BUILD_TYPE" = "full" ] || [ "$BUILD_TYPE" = "go" ]; then
    echo "=== Building Go Component ==="
    cd "$REMOTE_PATH/goyolo"
    
    echo "Cleaning..."
    rm -rf bin
    mkdir -p bin
    
    echo "Downloading dependencies..."
    go mod download
    go mod tidy
    
    echo "Building..."
    CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
        -o bin/goyolo-service \
        -ldflags="-s -w" \
        .
    
    echo "✓ Go build completed"
    echo ""
fi

# Run tests
if [ "$BUILD_TYPE" = "full" ] || [ "$BUILD_TYPE" = "test" ]; then
    echo "=== Running Tests ==="
    cd "$REMOTE_PATH/goyolo"
    go test -v ./...
    echo "✓ Tests completed"
    echo ""
fi

# Verify binaries
echo "=== Verifying Binaries ==="
echo ""
echo "C++ Binaries:"
if [ -f "$REMOTE_PATH/YOLOv8-OpenVINO-CPP-Inference/bin/detect" ]; then
    echo "✓ detect"
    file "$REMOTE_PATH/YOLOv8-OpenVINO-CPP-Inference/bin/detect"
else
    echo "✗ detect not found"
fi

if [ -f "$REMOTE_PATH/YOLOv8-OpenVINO-CPP-Inference/bin/detect_service" ]; then
    echo "✓ detect_service"
    file "$REMOTE_PATH/YOLOv8-OpenVINO-CPP-Inference/bin/detect_service"
else
    echo "✗ detect_service not found"
fi

echo ""
echo "Go Binary:"
if [ -f "$REMOTE_PATH/goyolo/bin/goyolo-service" ]; then
    echo "✓ goyolo-service"
    file "$REMOTE_PATH/goyolo/bin/goyolo-service"
else
    echo "✗ goyolo-service not found"
fi

echo ""
echo "=== Build Summary ==="
echo "C++ binaries: $(ls -lh $REMOTE_PATH/YOLOv8-OpenVINO-CPP-Inference/bin/ 2>/dev/null | tail -2 | awk '{print $9, $5}')"
echo "Go binary: $(ls -lh $REMOTE_PATH/goyolo/bin/goyolo-service 2>/dev/null | awk '{print $9, $5}')"
echo ""
echo "✓ Compilation completed successfully!"
COMPILE_EOF

# Upload script
log_info "Uploading compilation script..."
sshpass -p "$REMOTE_PASSWORD" scp -o StrictHostKeyChecking=no \
    "$COMPILE_SCRIPT" "${REMOTE_USER}@${REMOTE_HOST}:/tmp/compile.sh" > /dev/null

# Execute
log_info "Executing compilation..."
echo ""
sshpass -p "$REMOTE_PASSWORD" ssh -o StrictHostKeyChecking=no \
    "${REMOTE_USER}@${REMOTE_HOST}" "bash /tmp/compile.sh '$BUILD_TYPE'"

# Cleanup
rm -f "$COMPILE_SCRIPT"

echo ""
log_success "Compilation completed!"

